package com.jbm.jbmsupplier.model;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.app.ProgressDialog;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;

import com.jbm.jbmsupplier.ui.FieldEditActivity;
import com.jbm.jbmsupplier.util.*;

public class JobInfo extends Job {
	public ArrayList<fileInfo> fileList = new ArrayList<fileInfo>();
	private boolean mIsLoad = false;

	public JobInfo(Job job)
	{
		this.address = job.address;
		this.jobid = job.jobid;
		this.catalog = job.catalog;
		this.create_time = job.create_time;
		this.houseno = job.houseno;
		this.mediacount = job.mediacount;
		this.medialist = job.medialist;
		this.mobile = job.mobile;
		this.repaircontent = job.repaircontent;
		this.rdesc = job.rdesc;
		this.rtype = job.rtype;
		this.status = job.status;
		this.team = job.team;
		this.user = job.user;
		this.name = job.name;
	}
	public String getXmlFileName()
	{
		String fileName = Integer.toString(this.jobid);
		File skRoot = Environment.getExternalStorageDirectory();
		String pathString = skRoot.getPath()+ File.separator+"mediafileInfo"+File.separator+fileName+".xml";
		return pathString;
	}

	public fileInfo getFileInfo(int index)
	{
		if(fileList.size() > index)
		{
			fileInfo fInfo = fileList.get(index);
			return fInfo;
		}
		return null;
	}
	
	void addFileInfo(fileInfo fInfo)
	{
		fileList.add(fInfo);
	}
	
	public int getFileCount()
	{
		return fileList.size();
	}
	
	public void removeFileInfo(fileInfo fInfo)
	{
		fileList.remove(fInfo);
		fInfo.Remove();
	}

	public String getGuid() {
		return Integer.toString(this.jobid);
	}
	
	public void ReadXml()
	{
		if(this.mIsLoad)
			return;
		String xmlFileString = getXmlFileName();
		try
		{
			boolean findxml = false;
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser parser = factory.newSAXParser();
			XMLReader reader = parser.getXMLReader();
			XmlRead xmlRead = new XmlRead();
			reader.setContentHandler(xmlRead);
			File skRoot = Environment.getExternalStorageDirectory();
			File xmlFile = new File(xmlFileString);
			if(xmlFile.exists())
			{
				findxml = true;
			}
			if(findxml)
			{
				InputStream in = new FileInputStream(xmlFile);
				reader.parse(new InputSource(in));
			}
			this.mIsLoad = true;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	
	}
	
	public void XmlWrite()
	{
		XmlWrite write = new XmlWrite();
		write.writeXml(this);
	}
	
	public static Media ConvectToMedia(fileInfo info)
	{
		Media obj = new Media();
		switch(info.getType())
		{
		case fileInfo.TYPE_TEXT:
			obj.mtype="text";
			break;
		case fileInfo.TYPE_PIC:
			obj.mtype="pic";
			break;
		case fileInfo.TYPE_VIDEO:
			obj.mtype="video";
			break;
		case fileInfo.TYPE_SOUND:
			obj.mtype="audio";
			break;
		}
		obj.filename = info.getFileName();
		int lastSp = info.getPathName().lastIndexOf(File.separator);
		int length = info.getPathName().length();
		if(lastSp >= length-1)
		{
			obj.filepath = info.getPathName() + info.getFileName();
		}
		else
		{
			obj.filepath = info.getPathName() + File.separator + info.getFileName();
		}
		obj.text = info.getText();
		obj.m_memo = info.getDec();
		return obj;
	}
	
	public boolean upload(Handler handler,ProgressDialog dlg)
	{
		if(NetInterface.IsConnect())
		{
			NetInterface.UploadMedia(handler, dlg, this);
			return true;
		}
		return false;
	}
	
	public void clear()
	{
		this.fileList.clear();
		this.mediacount=0;
	}
	public void stopOrder(Handler handler) {
		// TODO Auto-generated method stub
		if(NetInterface.IsConnect())
		{
			NetInterface.StopOrder(this,handler);
		}
	}
	public void finishOrder(Handler handler) {
		if(NetInterface.IsConnect())
		{
			NetInterface.FinishOrder(this,handler);
		}
	}
	
	public void ConfirmJob(Handler handler)
	{
		if(NetInterface.IsConnect())
		{
			NetInterface.ConfirmJob(this,handler);
		}
		
	}
}
